#pragma once
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <iostream>
#include <unistd.h>
#include "user.hpp"
#include <unistd.h>
#include <mutex>
#include <thread>
#include <iostream>
void clientRead(int _sock,std::mutex &_mutex)
{
    while(true)
    {
        //接收消息
        char buffer[1024];
        sleep(1);
//std::cout<<"接收"<<std::endl;      
        _mutex.lock();
        ssize_t n = recv(_sock, buffer, sizeof(buffer), MSG_DONTWAIT);
        
//std::cout<<"接收成功"<<n<<std::endl;

        if (n >= 0)
        {
            buffer[n] = 0;
            std::cout << buffer << std::endl;
        }
        else if(n < 0 && n != -1)
        {
            std::cout << n << ":break" << std::endl;
            break;
        }
        _mutex.unlock();
    }
}
class tcpClient
{
public:
    tcpClient(const std::string ServerIp, const uint16_t ServerPort) : _sock(-1), _ServerPort(ServerPort), _ServerIp(ServerIp)
    {}

    void initClient()//创建套接字
    {
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            std::cerr << "socket create error" << std::endl;
            exit(2);
        }
    }
    void start()
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(_ServerPort);//??
        server.sin_addr.s_addr = inet_addr(_ServerIp.c_str());

        if(connect(_sock, (struct sockaddr*)&server, sizeof(server)) != 0)
        {
            std::cerr << "socket connect error" << std::endl;
            perror("connect");
        }
        else
        {
            //初始化当前用户信息
            USER::userMsg local;
            std::cout << "请输入你的名字" << std::endl;
            char buffer[10];
            std::cin>>buffer;
            std::cout << "请输入你的本地IP" << std::endl;
            char ip[15];
            std::cin>>ip;
            strcpy(local._name,buffer);
            strcpy(local._IP,ip);
            local._sock = _sock;
              
            //发送用户信息 -> 用于服务端验证用户
            if (send(_sock,(void*)&local,sizeof(local),0)< 0) 
            {
                std::cerr << "Failed to send struct." << std::endl;
                return;
            }
            
            //读消息
            std::unique_ptr<std::thread> tmp(new std::thread(clientRead, _sock, std::ref(_mutex)));
            tmp->detach();

            //开始聊天
            std::string content;
            while (true)
            {

                //发送消息
                sleep(1);
                _mutex.lock();
                std::cout << "Enter# ";
                std::getline(std::cin, content);
                std::string msg = "[";
                msg += local._name;
                msg += "]: ";
                msg += content;
                
                if(send(_sock,msg.c_str(),msg.size(),0) < 0)
                {
                    std::cerr << "Failed to msg." << std::endl;
                    return;
                }
                _mutex.unlock();
                                       
            }
        }
        //下线用户
        close(_sock);
    }

private:
    int _sock;
    std::mutex _mutex;
    uint16_t _ServerPort;
    std::string _ServerIp;
};
